package com.jason.kotlin.immersion

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import com.jason.kotlin.R
import com.jason.library.common.utils.ToastUtils
import com.jason.library.immersion.Immersion
import com.jason.library.immersion.model.BarParams
import kotlinx.android.synthetic.main.activity_status_bar.*

/**
 * Description:
 *
 * @author chenbaoyang
 * @create 2021/1/12 11:32
 */
class StatusBarActivity : AppCompatActivity() {

    private var time = 0L
    private var lastTime = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_status_bar)
        setRootClick()
        setApply()
    }

    private fun setRootClick() {
        rootLayout.setOnClickListener {
            lastTime = time;
            time = System.currentTimeMillis();
            if (time - lastTime < 300L) {
                // TODO: 2021/1/12 显示选择面板
                controlVisibility(View.VISIBLE)
                tvTips.visibility = View.GONE
                lastTime = 0L
                time = 0L
            }
        }
    }


    private fun controlVisibility(visibility: Int) {
        rgType.visibility = visibility
        rgFitWindow.visibility = visibility
        rgLight.visibility = visibility
        rgColor.visibility = visibility
        tvApply.visibility = visibility
    }

    private fun setApply() {
        tvApply.setOnClickListener {
            val params = when (rgColor.checkedRadioButtonId) {
                R.id.rbGradient ->
                    BarParams().drawableBar(R.drawable.bg_gradient).fitWindow(getFitWindow())
                R.id.rbTransparent ->
                    BarParams().transparentStatusBar(true).transparentNavigationBar(true)
                else ->
                    BarParams().statusBarColor(getColor()).navigationBarColor(getColor()).fitWindow(getFitWindow())

            }
            when (rgType.checkedRadioButtonId) {
                R.id.rbStatus -> Immersion.getInstance().with(this@StatusBarActivity).barParams(params).apply()
                R.id.rbNavigation -> { Immersion.getInstance().with(this@StatusBarActivity).barParams(params).apply() }
            }
        }
    }


    private fun getLight(): Boolean = when (rgLight.checkedRadioButtonId) {
        R.id.rbLight -> true
        R.id.rbNoLight -> false
        else -> false
    }

    private fun getFitWindow(): Boolean = when (rgFitWindow.checkedRadioButtonId) {
        R.id.rbFitWindow -> true
        R.id.rbNoFitWindow -> false
        else -> false
    }


    @ColorInt
    private fun getColor(): Int = when (rgColor.checkedRadioButtonId) {
        R.id.rbRed -> Color.RED
        R.id.rbCyan -> resources.getColor(R.color.cyan)
        R.id.rbAlphaBlack -> resources.getColor(R.color.alphaBlack)
        R.id.rbAlphaGreen -> resources.getColor(R.color.alphaGreen)
        else -> Color.TRANSPARENT
    }

    override fun onBackPressed() {
        if (tvApply.visibility == View.VISIBLE) {
            controlVisibility(View.INVISIBLE)
            return
        }
        super.onBackPressed()
    }

    private fun showToast(text: String) {
        Toast.makeText(this@StatusBarActivity, text, Toast.LENGTH_SHORT).show()
    }


}